package com.vhall.component.plugin.common.manager;

import com.vhall.component.plugin.common.constant.RedisKey;
import com.vhall.component.plugin.common.thrift.common.UserTicketWrapper;
import com.vhall.component.plugin.common.thrift.common.WebUserTicket;
import com.vhall.component.plugin.common.utils.JsonUtil;
import com.vhall.component.plugin.common.utils.ThriftCompactSerializerUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author yuanzh
 */
@Slf4j
@Component
public class CommonCacheManager {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public UserTicketWrapper getUserTicket(long userId) {
        UserTicketWrapper userTicketWrapper = null;
        try {
            String cacheValue = redisTemplate.opsForValue().get(RedisKey.USER_TICKET_KEY_PREFIX + userId);
            if (Objects.nonNull(cacheValue) && cacheValue.length() > 0) {
                userTicketWrapper = new UserTicketWrapper();
                ThriftCompactSerializerUtil.getInstance()
                        .deserializer(userTicketWrapper, cacheValue.getBytes(StandardCharsets.UTF_8));
            }
        } catch (Exception e) {
            log.error("getTicket error, userId={}", userId, e);
        } finally {
            log.info("getTicket userId={}, userTicketWrapper={}", userId,
                    Objects.nonNull(userTicketWrapper) ? userTicketWrapper.toString() : "null");
        }
        return userTicketWrapper;
    }

    public boolean saveUserTicket(UserTicketWrapper userTicketWrapper, Integer expireHour) {
        try {
            if (Objects.nonNull(userTicketWrapper) && userTicketWrapper.getUserId() > 0) {
                String redisKey = RedisKey.USER_TICKET_KEY_PREFIX + userTicketWrapper.getUserId();
                final byte[] redisValue = ThriftCompactSerializerUtil.getInstance().serializer(userTicketWrapper);

                int defaultExpireHour = 24;
                if (Objects.nonNull(expireHour)) {
                    defaultExpireHour = expireHour;
                }

                redisTemplate.opsForValue()
                        .set(redisKey, new String(redisValue, StandardCharsets.UTF_8.displayName()), defaultExpireHour,
                                TimeUnit.HOURS);

                return true;
            }
        } catch (Exception e) {
            log.error("saveUserTicket error, result={}", true, e);
        } finally {
            log.info("saveUserTicket userId={}, UserTicketWrapper={}",
                    Objects.nonNull(userTicketWrapper) ? userTicketWrapper.getUserId() : 0,
                    Objects.nonNull(userTicketWrapper) ? userTicketWrapper.toString() : "null");
        }
        return false;
    }

    public static void main(String[] args) {
        UserTicketWrapper userTicketWrapper = new UserTicketWrapper();
        userTicketWrapper.setUserId(1);

        WebUserTicket webUserTicket = new WebUserTicket();
        webUserTicket.setC2("c2");
        userTicketWrapper.setWebTicket(webUserTicket);

        System.out.println(JsonUtil.toJsonString(userTicketWrapper));
    }
}
